/**
 * Copyright © 2017 陈锦韬  All rights reserved.
 * 
 * @Package: com.application.format 
 * @author: Administrator   
 * @date: 2017年12月17日 下午5:42:43
 */
package com.application.format;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.application.common.BaseContext;
import com.application.common.BaseLogger;
import com.application.common.FastJsonMapper;
import com.application.iface.IMessage;
import com.application.iface.IPacket;


/**
 * @className: JsonPacket 
 * @author: Administrator
 * @Description: TODO
 * @date: 2017年12月17日 下午5:42:43
 */
@Component("JsonPacket")
public class JsonPacket implements IPacket {

	/* (non-Javadoc)
	 * @see com.application.iface.IPacket#getMessage(java.lang.String, java.lang.String)
	 */
	@Override
	public IMessage getMessage(String msgClazName, String data) throws Exception {
		Map<String,IMessage> allMessage = BaseContext.context.getBeansOfType(IMessage.class);
		if(allMessage == null){
			throw new Exception("没有注册消息格式类型");
		}
		for(IMessage msg :allMessage.values()){
			if(msg.getClass().getSimpleName().equals(msgClazName)){
				return (IMessage)FastJsonMapper.fromJson(data, msg.getClass());
			}
		}
		
		throw new Exception("没有注册("+msgClazName+")消息格式类型");
	}

	/* (non-Javadoc)
	 * @see com.application.iface.IPacket#getData(java.lang.String, com.application.iface.IMessage)
	 */
	@Override
	public String getData(IMessage msg) throws Exception {
		String jsonStr = FastJsonMapper.toJson(msg);
		return jsonStr;
	}

}
